package edu.ucdavis.gwt.gis.client.toolbar;


import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.Widget;

import edu.ucdavis.cstars.client.MapWidget;
import edu.ucdavis.cstars.client.control.Control;
import edu.ucdavis.cstars.client.event.MapResizeHandler;
import edu.ucdavis.cstars.client.geometry.Extent;
import edu.ucdavis.gwt.gis.client.DataManager;
import edu.ucdavis.gwt.gis.client.GisClient;
import edu.ucdavis.gwt.gis.client.MapEventStatusHandler;
import edu.ucdavis.gwt.gis.client.config.GadgetConfig;
import edu.ucdavis.gwt.gis.client.gmaps.GoogleSearchPanel;
import edu.ucdavis.gwt.gis.client.query.QueryTool;
import edu.ucdavis.gwt.gis.client.query.QueryToolSettings;
import edu.ucdavis.gwt.gis.client.resources.GadgetResources;
import edu.ucdavis.gwt.gis.client.toolbar.button.BaseMapButton;
import edu.ucdavis.gwt.gis.client.toolbar.button.ToolbarItem;
import edu.ucdavis.gwt.gis.client.toolbar.menu.PopupMenu;
import edu.ucdavis.gwt.gis.client.toolbar.menu.PrintMenu;
import edu.ucdavis.gwt.gis.client.toolbar.menu.ToolbarMenu;

public class Toolbar extends Control {

	private static UpperToolbarUiBinder uiBinder = GWT.create(UpperToolbarUiBinder.class);
	interface UpperToolbarUiBinder extends UiBinder<Widget, Toolbar> {}
	
	protected BaseMapButton basemapSelector;
	private GoogleSearchPanel gSearch = null;
	private EsriSearchPanel eSearch = null;
	
	public PopupMenu popupMenu = new PopupMenu("Actions");
	protected HTML popupMenuButton = new HTML("Actions");
	
	@UiField HorizontalPanel menuPanel;
	@UiField SimplePanel searchPanel;
	@UiField HorizontalPanel statusPanel;
	@UiField SimplePanel messagePanel;
	@UiField SimplePanel customPanel;
	@UiField HTMLPanel root;
	
	public void setMenuText(String html) {
		popupMenuButton.setHTML(html);
	}

	public Toolbar() {
		initWidget(uiBinder.createAndBindUi(this));
		setStyleName(GadgetResources.INSTANCE.css().toolbar());

		if( GisClient.isIE7() || GisClient.isIE8() ) {
			getElement().getStyle().setProperty("borderBottom", "1px solid #aaaaaa");
		}
	}
	
	public BaseMapButton getBasemapSelector() {
		return basemapSelector;
	}
	
	public void addToolbarMenu(ToolbarMenu menu) {
		menu.init(popupMenu);
		popupMenu.addMenu(menu);
//		menuPanel.add(menu);
		menu.onAdd(this);
	}
	
	public void addToolbarItem(ToolbarItem item) {
		item.init();
		menuPanel.add(item);
		item.onAdd(this);
	}
	
	public void addStatusItem(ToolbarItem item) {
		item.init();
		statusPanel.add(item);
		item.onAdd(this);
	}

	public void addCustomItem(Widget item) {
		customPanel.add(item);
	}

	@Override
	public void init(MapWidget mapWidget) {
		setWidth(mapWidget.getWidth()+"px");
		mapWidget.addResizeHandler(new MapResizeHandler(){
			@Override
			public void onMapResize(Extent extent, int width, int height) {
				setWidth(width+"px");
			}
		});
		
		// add base map selector
		basemapSelector = new BaseMapButton();
		basemapSelector.init(mapWidget);
		BasemapGallery.INSTANCE.getDisableCheckbox().getElement().getStyle().setMarginRight(-5, Unit.PX);
		menuPanel.add(BasemapGallery.INSTANCE.getDisableCheckbox());
		addToolbarItem(basemapSelector);		
		addSpacer();
		if( DataManager.INSTANCE.getPrintPage() != null ) {
			addToolbarMenu(new PrintMenu());
		}
		
		popupMenuButton.addClickHandler(new ClickHandler(){
			@Override
			public void onClick(ClickEvent event) {
				if( !popupMenu.isShowing() ) popupMenu.showRelativeTo(popupMenuButton);
			}
		});
		popupMenuButton.setStyleName("toolbarButton");
		menuPanel.add(popupMenuButton);
		
		addSpacer();
		
		GadgetConfig config = DataManager.INSTANCE.getConfig();
		
		// setup query tool
		if( config.enabledQueryTool() ) {
			DataManager.INSTANCE.setQueryTool(new QueryTool(mapWidget));
			QueryToolSettings.INSTANCE.setMap(mapWidget);
			menuPanel.add(QueryToolSettings.INSTANCE.getToolbarIcon());
			addSpacer();
		}
		
		addStatusItem(MapEventStatusHandler.INSTANCE.getStatusButton());
		
		// SEARCH CONTROL 
		
		if( config.useGmaps() ){
			gSearch = new GoogleSearchPanel(config.getGMapsKey());
			searchPanel.add(gSearch);
		} else if ( !config.useCustomLocator() ){
			eSearch = new EsriSearchPanel();
			searchPanel.add(eSearch);
		}
		
		// Search
		if( gSearch != null ) gSearch.init(mapWidget);
		if( eSearch != null ) eSearch.init(mapWidget); 
		
		moveZoomSlider(DataManager.INSTANCE.getMap().getId());
	}
	
	public void setLocator(Composite l) {
		searchPanel.add(l);
	}
	
	public void addSpacer() {
		HTML spacer = new HTML("&nbsp;");
		spacer.setStyleName("toolbarSpacer");
		menuPanel.add(spacer);
	}
	
	public void setMessage(Widget message) {
		messagePanel.clear();
		messagePanel.add(message);
	}
	
	protected native void moveZoomSlider(String id) /*-{
		$wnd.document.getElementById(id+"_zoom_slider").style.top = "50px";
	}-*/;

}
